/**
 * 用户数据模型
 */
let crypto = require('crypto')

let jwtUtils = require('../utils/jwtUtils')
module.exports = class User extends require('./model1') {

    /**
     * 根据用户名和密码验证登录
     * 获取用户数据
     */
    static getUser(name, pwd) {
        return new Promise((resolve, reject) => {

            let md5 = crypto.createHash('md5')
            let newpwd = md5.update(pwd).digest('hex')
            let sql = "select * from user where username= '" + name + "' and password ='" + newpwd + "'"
            this.query(sql).then(result => {
                resolve(result);
            }).catch(err => {
                console.log("登录失败")
                reject(err);
            })
        })
    }


    static getUserNameTmp(username) {
        return new Promise((resolve, reject) => {
            let sql = "select id from user where username = ?"
            this.query(sql, username).then(result => {
                console.log(result)
                resolve(result);
            }).catch(err => {
                reject(err);
            })

        })
    }


    /**
     * 注册写入
     * @param UserData
     * @returns {Promise<any>}
     */
    static setUser(UserData) {
        return new Promise((resolve, reject) => {

            let sql = "insert into user (username,password,head) values(?,?,?)"
            this.query(sql, this.formatParams(UserData.username, UserData.password, '2020080517084240084.jpg')).then(result => {
                console.log(result)
                resolve(result);
            }).catch(err => {
                reject(err);
            })
        })

    }


    static getUserMailboxTmp(emil) {
        return new Promise((resolve, reject) => {
            let sql = "select id from user where mailbox = ?"
            this.query(sql, emil).then(result => {
                console.log(result)
                resolve(result);
            }).catch(err => {
                reject(err);
            })

        })
    }

    /**
     * 写入邮件
     */
    static setEmil(emil, u_id, token) {
        return new Promise((resolve, reject) => {
            let sql = 'UPDATE `user` SET mailbox="' + emil + '",token ="' + token + '"  WHERE id = ' + u_id
            // let sql="insert into user (token,mailbox) values(?,?)"
            this.query(sql).then(result => {
                console.log(result)
                resolve(result);
            }).catch(err => {
                reject(err);
            })
        })

    }

    /**
     * 写入头像
     * @param head_img
     */
    static upUserHead(head_img, u_id) {
        return new Promise((resolve, reject) => {
            let sql = 'UPDATE `user` SET head="' + head_img + '"  WHERE id = ' + u_id
            // let sql="insert into user (token,mailbox) values(?,?)"
            this.query(sql).then(result => {
                console.log(result)
                resolve(result);
            }).catch(err => {
                reject(err);
            })
        })

    }

    /**
     * 修改名字
     * @param head_img
     * @param u_id
     * @returns {Promise<any>}
     */
    static upUserName(name, u_id) {
        return new Promise((resolve, reject) => {
            let sql = 'UPDATE `user` SET username="' + name + '"  WHERE id = ' + u_id
            // let sql="insert into user (token,mailbox) values(?,?)"
            this.query(sql).then(result => {
                console.log(result)
                resolve(result);
            }).catch(err => {
                reject(err);
            })
        })

    }

    static getUserNamebyId(u_id) {
        return new Promise((resolve, reject) => {
            let sql = 'select username from user where id = ?'
            this.query(sql, u_id).then(result => {
                console.log(result)
                resolve(result);
            }).catch(err => {
                reject(err);
            })

        })
    }

    static getUserAllbyId(u_id) {
        return new Promise((resolve, reject) => {
            let sql = 'select * from user where id = ?'
            this.query(sql, u_id).then(result => {
                console.log(result)
                resolve(result);
            }).catch(err => {
                reject(err);
            })

        })
    }

    /**
     * 获取所有的user名和id对应表
     * @returns {Promise<any>}
     */
    static getUserNameAll() {
        return new Promise((resolve, reject) => {
            let sql = 'select username ,id from user '
            this.query(sql).then(result => {
                console.log(result)
                resolve(result);
            }).catch(err => {
                reject(err);
            })

        })
    }


}
